package com.asha.md360player4android.vr.projection;

import android.content.Context;
import android.opengl.Matrix;

import com.asha.md360player4android.vr.objects.MDCircleExtra;
import com.asha.vrlib.MD360Director;
import com.asha.vrlib.MD360DirectorFactory;
import com.asha.vrlib.model.MDMainPluginBuilder;
import com.asha.vrlib.model.MDPosition;
import com.asha.vrlib.objects.MDAbsObject3D;
import com.asha.vrlib.objects.MDObject3DHelper;
import com.asha.vrlib.plugins.MDAbsPlugin;
import com.asha.vrlib.plugins.MDPanoramaPlugin;
import com.asha.vrlib.strategy.projection.AbsProjectionStrategy;

/**
 * Created by hzqiujiadi on 16/6/26.
 * hzqiujiadi ashqalcn@gmail.com
 */
public class FYCircleProjection extends AbsProjectionStrategy {

    private static final MDPosition position = MDPosition.newInstance().setZ(-2f);
    private MDAbsObject3D object3D;

    public FYCircleProjection() {
    }

    public static FYCircleProjection create() {
        return new FYCircleProjection();
    }

    @Override
    public void turnOnInGL(Context context) {
        object3D = new MDCircleExtra();
        MDObject3DHelper.loadObj(context, object3D);
    }

    @Override
    public void turnOffInGL(Context context) {

    }

    @Override
    public boolean isSupport(Context context) {
        return true;
    }

    @Override
    public MDAbsObject3D getObject3D() {
        return object3D;
    }

    @Override
    public MDPosition getModelPosition() {
        return position;
    }

    @Override
    public MDAbsPlugin buildMainPlugin(MDMainPluginBuilder builder) {
        return new MDPanoramaPlugin(builder);
    }

    @Override
    protected MD360DirectorFactory hijackDirectorFactory() {
        return new OrthogonalDirectorFactory();
    }

    private class OrthogonalDirectorFactory extends MD360DirectorFactory {
        @Override
        public MD360Director createDirector(int index) {
            return new OrthogonalDirector(new MD360Director.Builder());
        }
    }

    private class OrthogonalDirector extends MD360Director {

        private OrthogonalDirector(Builder builder) {
            super(builder);
            setEyeZ(-200);
        }

        @Override
        public void setDeltaX(float mDeltaX) {
            // nop
        }

        @Override
        public void setDeltaY(float mDeltaY) {
            // nop
        }

        @Override
        public void updateSensorMatrix(float[] sensorMatrix) {
            // nop
        }

        @Override
        protected void updateProjection() {
            final float left = -0.5f;
            final float right = 0.5f;
            final float bottom = -0.5f;
            final float top = 0.5f;
            final float far = 500;
            Matrix.orthoM(getProjectionMatrix(), 0, left, right, bottom, top, getNear(), far);
        }
    }

}
